import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';

@Component({
  selector: 'app-user-rent',
  templateUrl: './user-rent.component.html',
  styles: []
})
export class UserRentComponent implements OnInit {
  @ViewChild('st', {static: false})st: TablePageComponent;
  @Input()
  set param(value:any){
    console.log({value});
    if(!value) return;
    this._param = value;
  }
  _param:any;
  config: SPage = {
    columns: [
      {
        index: 'roomFloor',
        title: '所在层数',
      },
      {
        index: 'roomNumber',
        title: '门牌号',
      },
      {
        index: 'roomName',
        title: '房间名称',
      },
      {
        index: 'houseName',
        title: '区域名称',
      },
      {
        index: 'roomApprove',
        title: '审批状态',
        type: ColumnType.tag,
        tag: {
          0: {text:'未审批', color: 'red'},
          1: {text:'已审批', color: 'green'},
        }
      },
      {
        index: 'roomRemark',
        title: '备注'
      },
    ],
    data:[],
    editBtn: [
      { title : "退租"}
    ],
    ps: 5,
  };

  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modal: NzModalService
  ) { }

  ngOnInit() {
    this.getRentList();
  }

  operate(e: any): void {
    const self = this;
    if (e.title === '退租') {
      this.modal.confirm({
        nzTitle: '确定退租？',
        nzOnOk: () => {
          const body = {
            peopleId : self._param.id,
            roomId : e.data.id
          };
          console.log(body);
          this.http.post(this.api.user.userSurrender.url,body).subscribe((res:any) => {
            if(res.status === '200'){
              this.msg.success('退租成功');
              this.getRentList();
            }else{
              this.msg.error(res.message);
            }
          })
        }
      })
    }
  }
 

  getRentList(){
    this.http.post('/core/room/findPersonAndRoom', {id:this._param.id}).subscribe((res:any) => {
      console.log(res)
      if (res.status == '200') {
        this.config.data = res.data;
      }
    })
  }

}
